<?php
//data/init.php
//创建到服务器的连接，连接数据库  mysqli_connect(IP, 用户名，密码，数据库名)  连接数据库  
$link = @mysqli_connect('127.0.0.1', 'root', '', 'test');
if(!$link) die('连接失败');
mysqli_query($link,"set names 'utf8'");
// 准备sql语句
$sql = "select * from indexlist";
// echo  $sql;
// 执行sql语句
$res = mysqli_query($link, $sql);
//  echo mysqli_fetch_assoc($res);
$arr = [];
while ($row = mysqli_fetch_assoc($res)) {
  $arr[] = $row;
}
if (mysqli_num_rows($res) > 0) {
  // print_r($str);
  $str = json_encode(array(
    "code" => 200,
    "data" => $arr,
    "msg" => null
  ));
  echo $str;
} else {
  $str = json_encode(array(
    "code" => 500,
    "data" => $arr,
    "msg" => "没有数据"
  ));
  echo $str;
}